import {
	getEventType as apiGetEventType,
	getEventCategory as apiGetEventCategory,
} from "@/api/Interface/event"
import {
	DicEnum,
	ClassTypeEnum
} from "@/enums/event"

/**
 * @description 获取事件类别(事件类型选择级联父节点数据===>按事件分类,按部件类型...) 
 */
export const useGetEventType = () => {
	return new Promise((resolve, reject) => {
		try {
			apiGetEventType({
				pageNum: 1,
				pageSize: 99999,
				dictType: DicEnum.sjlx
			}).then(res => {
				if (res.code === 200) {
					const list = res.rows.filter(f => f.dictValue !== "0");
					resolve({
						data: useFormatDatas(list, {
							label: "dictLabel",
							value: "dictValue"
						})
					})
				} else {
					resolve({
						data: []
					})
				}
			})
		} catch (e) {
			//TODO handle the exception
			reject({
				data: []
			})
		}
	})
}
/**
 * @description 获取事件大类小类 (事件类型选择级联子孙节点数据) 
 */
export const useGetEventCategory = async (params) => {
	return new Promise(async (resolve, reject) => {
		try {
			const {
				code,
				rows
			} = await apiGetEventCategory(params)
			if (code === 200) {
				const list = rows;
				resolve({
					data: useFormatDatas(list, {
						label: "eventTypeName",
						value: "eventTypeId"
					})
				})
			} else {
				resolve({
					data: []
				})
			}
		} catch (e) {
			//TODO handle the exception
			reject({
				data: []
			})
		}
	})
}

export const useFormatDatas = (list, {
	label,
	value
}) => {
	if (!list || list.length === 0) return [];
	list.forEach(item => {
		item.label = item[label];
		item.value = item[value];
	})
	return list;
}

/**
 * @description 事件类型级联组件点选回调
 * @param {Number} level 当前选择的层级
 * @param {Object} e 当前选择的数据对象
 * 
 */
// export const useCheckEventConfirm = async ({
// 	level,
// 	e
// }) => {
// 	// 父节点和子孙节点的接口不一样
// 	// 事件分类
// 	// flowType ：1 
// 	// eventTypeItem：0
// 	// 部件类型
// 	// flowType ：1
// 	// eventTypeItem 1 
// 	if (level === 0) {
// 		const params = {
// 			flowType: 1,
// 			eventTypeItem: e.dictValue === "1" ? 0 : 1,
// 			eventTypePid: ClassTypeEnum.big
// 		}
// 		await useGetEventCategory(params)
// 	} else if (level >= 1) {
// 		const params = {
// 			eventTypePid: e.eventTypeId
// 		}
// 		await useGetEventCategory(params)
// 	}
// }
